package com.wkx.security;/**
 * @className: MySecurityConfig
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/12
 **/

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @ClassName : MySecurityConfig  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/12  10:23
 */
@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private UserDetailsService userDetailsService;
    @Bean
    PasswordEncoder passwordEncoder(){
        //不对密码进行加密(在springsecurity5中默认需要进行密码加密)
        return NoOpPasswordEncoder.getInstance();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").defaultSuccessUrl("/admin/login").permitAll().and().csrf().disable();

        http.authorizeRequests()
                .antMatchers("/","/admin/login").permitAll()
                .antMatchers("/admin/*","/menu/**","/role/**").hasRole("ADMIN")
                .antMatchers("/admin/**").hasRole("USER_MANAGER")
                .antMatchers("/book/*").hasRole("PROD_MANAGER");


        http.exceptionHandling().accessDeniedPage("/403.html");

        http.logout().clearAuthentication(true).invalidateHttpSession(true);

        http.headers().frameOptions().sameOrigin();
    }
}
